package com.swak.frame.filter;

import java.lang.reflect.Method;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;

import com.swak.frame.dto.base.SysRestCode;
import com.swak.frame.exception.ApiExceptionUtil;
import com.swak.frame.filter.annotation.SwakMethod;
import com.swak.frame.filter.annotation.SwakService;
import com.swak.frame.logger.SwakLoggerFactory;

public class ApiAuthCodeUtil {

    private static Logger logger = SwakLoggerFactory.getLogger(ApiAuthCodeUtil.class);

    public static String getAuthCode(SwakService swakService, SwakMethod swakMethod,
        Method method) {
        String authCode = swakMethod.value();
        if (StringUtils.isEmpty(authCode)) {
            if (swakService == null) {
                logger.error("@SwakService注解未配置,className:{}",
                    new Object[] {method.getDeclaringClass().getName()});
                ApiExceptionUtil.throwException(SysRestCode.SYS_INVALID_SPI);
            } else {
                authCode = swakService.name() + "_" + method.getName();
            }
        }
        return authCode;
    }
}
